package com.gensdai.leliang.common.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gensdai.leliang.R;
import com.gensdai.leliang.common.bean.CategoryBean;
import com.gensdai.leliang.view.Toaster;
import com.gensdai.leliang.view.banner.SimpleMenu;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**首页功能按钮的适配
 * Created by LT on 2017/6/15.
 */

public class MainMenuViewPagerAdapterRec extends DelegateAdapter.Adapter<MainMenuViewPagerAdapterRec.MainViewHolder> {

    private Context mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;

    OnMenuClickLis click;

    List<CategoryBean> blist;

    List<List<CategoryBean>> vlist;

    public static int pagePosition = 0;

    public MainMenuViewPagerAdapterRec(Context context, LayoutHelper layoutHelper, List<CategoryBean> list) {
        this(context, layoutHelper, list, new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public MainMenuViewPagerAdapterRec(Context context, LayoutHelper layoutHelper, List<CategoryBean> list, @NonNull VirtualLayoutManager.LayoutParams layoutParams) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.blist = list;
        this.mLayoutParams = layoutParams;
        vlist = createList(blist,6);

    }

    //设置item的点击回调
    public void setOnMenuClick(OnMenuClickLis click){
        this.click = click;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_home_menu_viewpager, parent, false));
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, final int position) {
        holder.menu.setSource(vlist).startScroll();
        holder.menu.setOnMenuClickListener(new SimpleMenu.onMenuClickListner() {
            @Override
            public void onMenuClick(int id) {
                if(click != null){
                    click.OnMenuClick(id);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return vlist.size();
    }


    static class MainViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.viewpagerMenu)
        SimpleMenu menu;

        public MainViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
        }
    }



    public static List<List<CategoryBean>>  createList(List<CategoryBean> targe,int size) {
        List<List<CategoryBean>> listArr = new ArrayList<List<CategoryBean>>();
        //获取被拆分的数组个数
        int arrSize = targe.size()%size==0?targe.size()/size:targe.size()/size+1;
        for(int i=0;i<arrSize;i++) {
            List<CategoryBean>  sub = new ArrayList<CategoryBean>();
            //把指定索引数据放入到list中
            for(int j=i*size;j<=size*(i+1)-1;j++) {
                if(j<=targe.size()-1) {
                    sub.add(targe.get(j));
                }
            }
            listArr.add(sub);
        }
        return listArr;
    }

    /*本适配器的回调接口*/
    public interface OnMenuClickLis<T>{
        void OnMenuClick(T menu);
    }

}
